package com.dc.di.sf.dt;

import org.apache.axiom.om.OMElement;
import org.apache.commons.codec.binary.Base64;

import com.dc.di.sf.utils.OMUtils;

public class Attachment implements DTObject<Attachment> {

	public static final String TAG_ATTACHMENT = "Attachment";
	public static final String TAG_IDNAME = "IDName";
	public static final String TAG_DISPLAYNAME = "DisplayName";
	public static final String TAG_DESCRIPTION = "Description";
	public static final String TAG_TYPEDESCRIPTION = "TypeDescription";
	public static final String TAG_CONTENT = "Content";

	private String idName;
	private String displayName;
	private String description;
	private String typeDescription;
	private byte[] content;

	public Attachment() {

	}

	/**
	 * @return the idName
	 */
	public String getIdName() {
		return idName;
	}

	/**
	 * @param idName
	 *            the idName to set
	 */
	public void setIdName(String idName) {
		this.idName = idName;
	}

	/**
	 * @return the displayName
	 */
	public String getDisplayName() {
		return displayName;
	}

	/**
	 * @param displayName
	 *            the displayName to set
	 */
	public void setDisplayName(String displayName) {
		this.displayName = displayName;
	}

	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @param description
	 *            the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * @return the typeDescription
	 */
	public String getTypeDescription() {
		return typeDescription;
	}

	/**
	 * @param typeDescription
	 *            the typeDescription to set
	 */
	public void setTypeDescription(String typeDescription) {
		this.typeDescription = typeDescription;
	}

	/**
	 * @return the content
	 */
	public byte[] getContent() {
		return content;
	}

	/**
	 * @param content
	 *            the content to set
	 */
	public void setContent(byte[] content) {
		this.content = content;
	}

	public OMElement toXML() {
		OMElement data = OMUtils.createOMElement(TAG_ATTACHMENT);
		data.addChild(OMUtils.createOMElementWithText(TAG_IDNAME, idName));
		data.addChild(OMUtils.createOMElementWithText(TAG_DISPLAYNAME, displayName));
		data.addChild(OMUtils.createOMElementWithText(TAG_DESCRIPTION, description));
		data.addChild(OMUtils.createOMElementWithText(TAG_TYPEDESCRIPTION, typeDescription));
		data.addChild(OMUtils.createOMElementWithText(TAG_CONTENT, new String(new Base64().encode(content))));
		return data;
	}

	public Attachment fromXML(OMElement xml) {
		if (xml != null) {
			idName = OMUtils.getTextOfChild(xml, TAG_IDNAME);
			displayName = OMUtils.getTextOfChild(xml, TAG_DISPLAYNAME);
			description = OMUtils.getTextOfChild(xml, TAG_DESCRIPTION);
			typeDescription = OMUtils.getTextOfChild(xml, TAG_TYPEDESCRIPTION);
			content = new Base64().decode(OMUtils.getTextOfChild(xml, TAG_CONTENT).getBytes());
		}
		return this;
	}

	/* (non-Javadoc)
	 * @see com.dc.di.sf.dt.DTObject#toXMLString()
	 */
	public String toXMLString() {
		StringBuilder sb = new StringBuilder();
		sb.append(OMUtils.generateOMElementBeginString(TAG_IDNAME));
		sb.append(OMUtils.generateOMElementString(TAG_IDNAME, idName));
		sb.append(OMUtils.generateOMElementString(TAG_DISPLAYNAME, displayName));
		sb.append(OMUtils.generateOMElementString(TAG_DESCRIPTION, description));
		sb.append(OMUtils.generateOMElementString(TAG_TYPEDESCRIPTION, typeDescription));
		sb.append(OMUtils.generateOMElementString(TAG_CONTENT, new String(new Base64().encode(content))));
		sb.append(OMUtils.generateOMElementEndString(TAG_IDNAME));
		return sb.toString();
	}

}
